package serviceImpl;


import java.util.Date;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;
import model.Agenda;
import service.AgendaService;

public class AgendaServiceImpl extends GenericServiceImpl<Agenda> implements
		AgendaService {

	private static final long serialVersionUID = 8190224707124575009L;

	private Query query;

	public Agenda procuraAgendaBy(String title,Long codigo, Date inicio, Date fim) {
		query = getEm()
				.createQuery(
						"Select c from Agenda c where c.title = :title and c.usuario.codigo = :codigo and c.startDate = :inicio and c.endDate = :fim");
		query.setParameter("title", title);
		query.setParameter("codigo", codigo);
		query.setParameter("inicio", inicio);
		query.setParameter("fim", fim);
		try {
			return (Agenda) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}

	}
	
	@SuppressWarnings("unchecked")
	public List<Agenda> procuraAgendaHoje(Long codigo, Date inicio) {
		query = getEm()
				.createQuery(
						"Select c from Agenda c where c.usuario.codigo = :codigo and c.startDate =:inicio");
		query.setParameter("codigo", codigo);
		query.setParameter("inicio", inicio);
		try {
			return  (List<Agenda>) query.getResultList();
		} catch (NoResultException nre) {
			return null;
		}
		
		
	}
}
